package com.suneee.refund.job;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.suneee.base.util.ContextHelper;
import com.suneee.quartz.QuartzManager;
import com.suneee.quartz.dao.ScheduleConfigDao;
import com.suneee.quartz.entity.ScheduleConfig;
import com.suneee.shop.dao.ShopDao;
import com.suneee.shop.entity.Shop;

public class RefundApplyJob implements StatefulJob {

	private static final Logger logger = LoggerFactory.getLogger(RefundApplyJob.class);
	
	@Override
	@Transactional
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		logger.info("------进入调度任务--");

		ScheduleConfigDao scheduleConfigDao = ContextHelper.getBean("scheduleConfigDao", ScheduleConfigDao.class);
		ShopDao shopDao = ContextHelper.getBean("shopDao", ShopDao.class);
//		ScheduleTimeDao scheduleTimeDao = ContextHelper.getBean("scheduleTimeDao", ScheduleTimeDao.class);
		//获取平台所有店铺
		List<Shop> shopList = shopDao.selectWhere(" status = ? and enabled = 1 ", Shop.USE);
		ScheduleConfig dto = new ScheduleConfig();
		String bussinessGroup = context.getJobDetail().getGroup();
		dto.setBusinessGroup(bussinessGroup);
		//执行订单调度任务
		for (Shop shop : shopList) {
			ScheduleConfig config = null;
			if(config == null){
				//首先根据店铺id去查找对应的配置信息
				dto.setShopId(shop.getShopId());
				config = scheduleConfigDao.getScheduleConfig(dto);
				if(config == null){
					//再根据企业编码查询配置信息
					dto.setShopId(null);
					dto.setEnterpriseCode(shop.getEnterpriseCode());
					config = scheduleConfigDao.getScheduleConfig(dto);
					//最后查询平台配置信息
					if(config == null){
						dto.setEnterpriseCode("suneee");
						config = scheduleConfigDao.getScheduleConfig(dto);
					}
				}
				//得到配置信息，根据配置信息执行任务调度 
				if(config != null && config.getInterva() != null){
					Map<String,String> jobMap = new HashMap<String, String>();
					//String jobNameStr = shop.getRuleInfo()+"_"+shop.getShopId()+"/"+shop.getEnterpriseCode()+"*"+"job"+"-"+shop.getProxyURL()+"|"+config.getBusinessGroup();
					String jobName = shop.getShopId()+"_"+config.getBusinessGroup()+"_"+config.getConfigType();
					String jobGroupName = config.getBusinessGroup()+"_"+config.getConfigType()+"_group" ;//任务组名
					String triggerName  = shop.getShopId()+"_"+config.getBusinessGroup()+"_"+config.getConfigType()+"_trigger" ;//触发器名
					String triggerGroupName = shop.getShopId()+"_triggerGroup";//触发器组名
					
					jobMap.put("shopId", shop.getShopId().toString());
					
					try {
						if(!QuartzManager.checkIsExisted(jobName, jobGroupName))//检测job是否存在,不存在则加入job队列
							QuartzManager.addJob(jobName,  
									jobGroupName, 
									triggerName, 
									triggerGroupName, 
									RefundApplyDetailJob.class, 
									"0 */"+config.getInterva()+" * * * ?",
									jobMap);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						logger.error(e.getMessage(), e);
					}
				}
			}
		}
	}
}
